package sklepik;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import sklepik.model.Category;
import sklepik.modelcena.Item;

public class Main {

	private static final String rootDir = "C:\\Users\\wolak\\";
	private static final String imagesDir = rootDir + "zdjecia\\";

	private static Downloader downloader;

	public static void main(String[] args) throws JAXBException, IOException {

		File xmlFile = new File(rootDir + "pl_complete_01.xml");
		File xmlFileCeny = new File(rootDir + "ceny.xml");
		sklepik.modelcena.Root cenaRoot = (sklepik.modelcena.Root) unmarschalXml(xmlFileCeny, "sklepik.modelcena");
		sklepik.model.Shop sklepRoot = (sklepik.model.Shop) unmarschalXml(xmlFile, "sklepik.model");
		List<ResultRecord> resultRecords = new ArrayList<ResultRecord>();
		
		Map<BigInteger, String> map = createIdToCategoryMap(sklepRoot.getCategories().getCategory());
		Map<BigInteger, BigDecimal> mapPrice = createIdToPriceMap(cenaRoot.getItem());
		
		for (sklepik.model.Product inProduct : sklepRoot.getProduct()) {
			String nazwaPliku;
			String nowa;
			String kategorieString = "";
			StringTokenizer st = new StringTokenizer(inProduct.getImage(), "=");
			st.nextToken();
			nazwaPliku = st.nextToken();
			nowa = nazwaPliku.substring(0, nazwaPliku.length() - 5);
			nazwaPliku = imagesDir.concat(nowa);
			for (Category category : inProduct.getCategories().getCategory()) {
				kategorieString += (map.get(BigInteger.valueOf(Long.parseLong(category.getContent()))) + ";");
			}
			ResultRecord wyjsciowy = new ResultRecord(inProduct.getNazev(), inProduct.getPopis(), BigInteger.TEN,inProduct.getId(), inProduct.getWeight(), nowa, kategorieString,mapPrice.get(inProduct.getId()));
			//System.out.print(wyjsciowy.toString());
			resultRecords.add(wyjsciowy);
			
			//System.out.println(nowa);
			//downloader = new Downloader();
			//downloader.download(inProduct.getImage(), imagesDir, nowa);
			//String category = wejsciowy.getCategory().getContent();
			//sciagacz.download(wejsciowy.getImage(), nazwaPliku);
			//System.out.println(wejsciowy.getCategory().getId());

		}
		
		ResultExporter exporter = new ResultExporterCsvImpl();
		exporter.exportResultRecords(resultRecords);
	}

	private static Map<BigInteger, String> createIdToCategoryMap(List<Category> categories) {
		Map<BigInteger, String> map = new HashMap<BigInteger, String>();
		for (Category category : categories) {
			map.put(category.getId(), category.getName());
		}
		return map;
	}
	
	private static Map<BigInteger, BigDecimal> createIdToPriceMap(List<Item> items) {
		HashMap<BigInteger, BigDecimal> map = new HashMap<BigInteger, BigDecimal>();
		for (Item item : items) {
			map.put(item.getId(), item.getPrice());
		}
		return map;
	}

	private static Object unmarschalXml(File xmlFile, String packagee) throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(packagee);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		return unmarshaller.unmarshal(xmlFile);
	}

}
